package com.sunshine.lnuplus.view.Main;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.sunshine.lnuplus.R;
import com.sunshine.lnuplus.databinding.ActivityMainBinding;
import com.sunshine.lnuplus.utils.DialogFragmentHelper;
import com.sunshine.lnuplus.utils.FileHelper;
import com.sunshine.lnuplus.utils.PermissionHelper;
import com.sunshine.lnuplus.utils.StatusBarUtils;
import com.sunshine.lnuplus.utils.TagHelper;
import com.sunshine.lnuplus.utils.UpdateHelper;
import com.sunshine.lnuplus.view.About.AboutActivity;
import com.sunshine.lnuplus.view.BaseActivity;

public class MainActivity extends BaseActivity {

    private MainViewModel viewModel;
    public static ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        viewModel = ViewModelProviders.of(this).get(MainViewModel.class);

        if(viewModel.isEverydayBackground()) FileHelper.setBackground(getApplicationContext(),binding.background);
        else FileHelper.setUserBackground(getApplicationContext(),binding.background);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.transparent));
        StatusBarUtils.with(this).init();

        BottomNavigationView bottomNavigationView = binding.bottomNavigationView;
        NavController navController = Navigation.findNavController(this,R.id.fragment);
        AppBarConfiguration configuration = new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build();
        NavigationUI.setupActionBarWithNavController(this,navController,configuration);
        NavigationUI.setupWithNavController(bottomNavigationView,navController);

        firstStart();
        if(viewModel.isCheckUpdate()) UpdateHelper.checkUpdate(this,getSupportFragmentManager(),false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode== TagHelper.APPSETTING){
            if(viewModel.isEverydayBackground()) {
                FileHelper.setBackground(getApplicationContext(),binding.background);
            }else if(!FileHelper.setUserBackground(getApplicationContext(),binding.background)){
                binding.background.setImageDrawable(getResources().getDrawable(R.mipmap.background));
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        viewModel.setStart();
        switch (requestCode){
            case TagHelper.MAIN:
                String toast = "";
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) toast = "授予成功";
                else toast = "您未授予权限";
                Snackbar.make(binding.mainCoordinatorLayout,toast,Snackbar.LENGTH_SHORT).show();
                break;
        }
    }

    private void firstStart(){
        //第一次启动登录成功
        if(getIntent().getBooleanExtra("first_start_success",false))
            Snackbar.make(binding.mainCoordinatorLayout,getString(R.string.login_success),Snackbar.LENGTH_SHORT).show();

        if(viewModel.getFirstStart()){
            DialogInterface.OnClickListener positive = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                        ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},TagHelper.MAIN);
                }
            };
            DialogInterface.OnClickListener negative = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    viewModel.setStart();
                }
            };
            DialogFragmentHelper dialogFragmentHelper = DialogFragmentHelper.newInstance("权限申请","为了辽大+能够正常运行，现向您申请储存权限，请您授予","授权","取消",false,positive,negative);
            dialogFragmentHelper.show(getSupportFragmentManager(),"");
        }
    }
}
